package com.rongda.deploy.operation.deploy.dto;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * Create By WeiBin on 2020/10/26 10:23
 */

/**
 * 响应信息主体
 *
 * @param <T>
 * @author jhy
 */
@Builder
@ToString
@Accessors(chain = true)
@AllArgsConstructor
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code = 200;

    @Getter
    @Setter
    private String msg = "操作成功";


    @Getter
    @Setter
    private T data;

    public R() {
        super();
    }

    public R(T data) {
        super();
        this.data = data;
    }

    public R(T data, String msg) {
        super();
        this.data = data;
        this.msg = msg;
    }

    public R success(T data, String msg) {
        return R.builder().code(200).data(data).msg(msg).build();
    }

    public R success(T data) {
        return R.builder().code(200).data(data).msg("操作成功").build();
    }


    public R error(String msg) {
        return this.error(null, msg);
    }

    public R error(T data, String msg) {
        return R.builder().code(500).data(data).msg("操作失败：" + msg).build();
    }

    public R(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = 500;
    }
}
